import api from '../../../utils/axios/api'
import dmzUtil from '../../../utils/DmzUtil'
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)


let modules = {
    namespaced: true,

    state: {
        menu_data: []
    },
    getters: {},
    mutations: {
        ["getMenuList"](state, obj) {
            state.menu_data = obj;
        },

    },
    actions: {
        getMenuListAction(context, router) {
            return api.getHomeInfoApi().then((res) => {
                let {data: result} = res

                let menuList = result.data.menus.menuList;
                let routerList = result.data.menus.routerList;
                let userInfo = result.data.userInfoVO;
                // 将用户信息保存在缓存当中
                if (userInfo) {
                    dmzUtil.setSession("userInfo", userInfo);

                }
                if (menuList) {
                    context.commit("getMenuList", menuList);
                }
                if (routerList) {
                    //4.动态的生成路由
                    //4.1获取原来的路由
                    let oldRouter = router.options.routes;
                    routerList.forEach(item => {
                        item.component = (resolve) => require([`@/views${item.url}.vue`], resolve)
                        // console.log(item.component)
                        oldRouter[0].children.push(item);
                    })
                    //5.添加到现有路由里面
                    router.$addRoutes(oldRouter);
                }
            })

        }


    }
}

export default modules
